package com.xy.manage.action;



import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sun.org.apache.xml.internal.security.utils.Base64;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.web.CookieUtils;
import com.xy.core.web.WebErrors;
import com.xy.manage.model.Tmembernews;
import com.xy.manage.service.TmembernewsService;



@Controller
public class TmembernewsAction{
	private static final Logger log = LoggerFactory.getLogger(TmembernewsAction.class);
	public TmembernewsAction() {
	}
 
 
 	/**
	 * 用户爆料新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmembernewsAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tmembernews", new Tmembernews());
        setDict(model);
		return "tmembernewsEdit";
	}

	/**
	 * 用户爆料新增保存
	 * 
	 * @param tmembernews
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tmembernewsSave.do")
	public String save(Tmembernews tmembernews, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tmembernews, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tmembernews.setCreatetime(new Date());
		tmembernews = tmembernewsService.save(tmembernews);
		return "redirect:v_tmembernewsList.do";
	}

	/**
	 * 用户爆料删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tmembernewsDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tmembernewsService.remove(id);
			}
		}
		return "redirect:v_tmembernewsList.do";
	}

	/**
	 * 用户爆料编辑页面
	 * 
	 * @param newsid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmembernewsEdit.do")
	public String edit(long newsid, HttpServletRequest request, ModelMap model) {
		Tmembernews tmembernews = tmembernewsService.get(newsid);
		model.addAttribute("tmembernews", tmembernews);
        setDict(model);
		return "tmembernewsEdit";
	}

	/**
	 * 用户爆料详情
	 * 
	 * @param newsid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmembernewsDetail.do")
	public String detail(long newsid, HttpServletRequest request, ModelMap model) {
		Tmembernews tmembernews = tmembernewsService.get(newsid);
		model.addAttribute("tmembernews", tmembernews);
        setDict(model);
        String pic64 = Base64.encode(tmembernews.getTp());
		String img = "data:image/png;base64," + pic64;
		model.addAttribute("imgSource", img);
		return "tmembernewsView";
	}

	/**
	 * 用户爆料查询（分页）
	 * 
	 * @param tmembernews
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmembernewsList.do")
	public String listPage(Tmembernews tmembernews, Integer pageNo,
			HttpServletRequest request, ModelMap model,String orderby,String order) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tmembernewsService.getTmembernewsByPage(toMap(tmembernews),
				pageNo, CookieUtils.getPageSize(request),orderby,order);
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tmembernewsList";
	}

	/**
	 * 用户爆料查询
	 * 
	 * @param tmembernews
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmembernewsList1.do")
	public String list(Tmembernews tmembernews, HttpServletRequest request, ModelMap model) {
		List<Tmembernews> list = tmembernewsService.getTmembernews(toMap(tmembernews));
		model.addAttribute("list", list);
        setDict(model);
		return "tmembernewsList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tmembernews
	 * @return
	 */	private Map<String,Object> toMap(Tmembernews tmembernews){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tmembernews.getNewsid()!=null&&!tmembernews.getNewsid().equals("")){
			map.put("newsid", tmembernews.getNewsid());
		}
		if(tmembernews.getUsername()!=null&&!tmembernews.getUsername().equals("")){
			map.put("username", tmembernews.getUsername());
		}
		if(tmembernews.getContent()!=null&&!tmembernews.getContent().equals("")){
			map.put("content", tmembernews.getContent());
		}
		if(tmembernews.getType()!=null&&!tmembernews.getType().equals("")){
			map.put("type", tmembernews.getType());
		}
		if(tmembernews.getAuthor()!=null&&!tmembernews.getAuthor().equals("")){
			map.put("author", tmembernews.getAuthor());
		}
		if(tmembernews.getPhoneNo()!=null&&!tmembernews.getPhoneNo().equals("")){
			map.put("phoneno", tmembernews.getPhoneNo());
		}
		if(tmembernews.getLevel()!=null&&!tmembernews.getLevel().equals("")){
			map.put("level", tmembernews.getLevel());
		}
		if(tmembernews.getCreatetime()!=null&&!tmembernews.getCreatetime().equals("")){
			map.put("createtime", tmembernews.getCreatetime());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("typeMap", DictUtils.getDictByType(DictUtils.TMEMBERNEWS_TYPE));
      model.addAttribute("levelMap", DictUtils.getDictByType(DictUtils.TMEMBERNEWS_LEVEL));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tmembernews tmembernews, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TmembernewsService tmembernewsService;   
}